package tq.com.tqteacher.activity.newPublish;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQTWDYPublishModel;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQQNoManagerView;
import tq.com.tqteacher.view.viewhold.TQPublishResultViewHelper;
import tq.com.tqteacher.view.viewhold.TQPublishSaveButtonViewHold;

public class TQTWDYResultActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQPublishResultViewHelper mPublishResultViewHelper;
    private TQPublishSaveButtonViewHold mTQPublishSaveButtonViewHold;

    private TQQNoManagerView mTQQNoManagerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_twdyresult_layout);
        setStatusBar();

        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("提问&答疑");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQTWDYResultActivity.this.finish();
            }
        });

        findViewById(R.id.publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQPublishType.showPublishNextStep(TQTWDYResultActivity.this);
            }
        });


        if(mPublishResultViewHelper == null){
            mPublishResultViewHelper = new TQPublishResultViewHelper(findViewById(R.id.baseInfo));
        }

        mPublishResultViewHelper.setModifyClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onModifyContent();
            }
        });

        if(mTQPublishSaveButtonViewHold == null){
            mTQPublishSaveButtonViewHold = new TQPublishSaveButtonViewHold(findViewById(R.id.bottom));

            mTQPublishSaveButtonViewHold.setPublishClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublish();
                }
            });
        }

        findViewById(R.id.modify1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onModify();
            }
        });

        mTQQNoManagerView = findViewById(R.id.qnoRegion);

        if(TQPublishType.getSharePublishItem().getNeedloadRemote()){
//            loadDetailInfo();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshData();
    }


    private void onModifyContent(){
        Intent intent = new Intent(this, TQTWDYPublishActivity.class);
        startActivity(intent);
    }

    private void onModify(){
        Intent intent = new Intent(this, TQTWDYSetQNoActivity.class);
        startActivity(intent);
    }

    private void refreshData(){
        if(mPublishResultViewHelper != null){
            mPublishResultViewHelper.setTWDYResult(TQPublishType.getSharePublishItem());
        }

        TQTWDYPublishModel lModel = null;
        if(TQPublishType.getSharePublishItem() != null && TQPublishType.getSharePublishItem() instanceof TQTWDYPublishModel){
            lModel = (TQTWDYPublishModel)TQPublishType.getSharePublishItem();
            lModel.setModify(true);
        }
        if(mTQQNoManagerView != null){
            mTQQNoManagerView.setData(lModel.getRegionData());
        }

        if(TQPublishType.getSharePublishItem().isPublish()){
            mTQPublishSaveButtonViewHold.setAlreadyPublish();
        }
    }

    private void loadDetailInfo(){
//        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
//            @Override
//            public void result(TQNetResult pNetResult) {
//                if(!pNetResult.isSuccess()){
//                    AlertDialogHelper.show(TQTWDYResultActivity.this, pNetResult.getErrMsg());
//                    return;
//                }
//
//                if(pNetResult.getData() == null) return;
//                try {
//                    JSONObject lObject = pNetResult.getData().getJSONObject("material");
//                    JSONObject lObject1 = lObject.getJSONObject("material");
//                    int type = lObject1.getInt("category");
//                    if(type != TQPublishType.getSharePublishItem().getType()){
//                        return;
//                    }
//                    TQPublishType.getSharePublishItem().fromJson(lObject1);
//                    refreshData();
//
//                }catch (JSONException pE){
//                    pE.printStackTrace();
//                }
//            }
//        });

        TQPublishType.getSharePublishItem().getDetailFromServer(this, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                refreshData();
            }
        });
    }

    private void onPublish(){
        if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.optimize){
            AlertDialogHelper.show(this, "已同步更新");
        }else{
            TQPublishType.getSharePublishItem().onPublish(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQTWDYResultActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().setState(TQSystemDefine.PUBLISH_STATE_Publish);
                    mTQPublishSaveButtonViewHold.setAlreadyPublish();
                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                    TQTWDYResultActivity.this.finish();
                }
            });
        }

    }
}
